package com.ruoyi.puyu.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.puyu.domain.PuyuCoach;
import com.ruoyi.puyu.service.IPuyuCoachService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 教练Controller
 * 
 * @author wusongwen
 * @date 2024-05-01
 */
@RestController
@RequestMapping("/puyu/coach")
public class PuyuCoachController extends BaseController
{
    @Autowired
    private IPuyuCoachService puyuCoachService;

    /**
     * 查询教练列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:coach:list')")
    @GetMapping("/list")
    public TableDataInfo list(PuyuCoach puyuCoach)
    {
        puyuCoach.setDelFlag("0");
        boolean bolRole = SecurityUtils.hasRole(UserConstants.ROLE_COACH)
                || SecurityUtils.hasRole(UserConstants.ROLE_STUDENT)
                || SecurityUtils.hasRole(UserConstants.ROLE_PARENT);
        if (bolRole) {
            puyuCoach.setUserId(SecurityUtils.getUserId());
        }
        bolRole = SecurityUtils.hasRole(UserConstants.ROLE_MANAGER)
                || SecurityUtils.hasRole(UserConstants.ROLE_ADMIN);
        if (bolRole) {
            puyuCoach.setUserId(null);
        }
        startPage();
        List<PuyuCoach> list = puyuCoachService.selectPuyuCoachList(puyuCoach);
        return getDataTable(list);
    }

    /**
     * 导出教练列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:coach:export')")
    @Log(title = "教练", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PuyuCoach puyuCoach)
    {
        List<PuyuCoach> list = puyuCoachService.selectPuyuCoachList(puyuCoach);
        ExcelUtil<PuyuCoach> util = new ExcelUtil<PuyuCoach>(PuyuCoach.class);
        util.exportExcel(response, list, "教练数据");
    }

    /**
     * 获取教练详细信息
     */
    @PreAuthorize("@ss.hasPermi('puyu:coach:query')")
    @GetMapping(value = "/{coachId}")
    public AjaxResult getInfo(@PathVariable("coachId") Long coachId)
    {
        return success(puyuCoachService.selectPuyuCoachByCoachId(coachId));
    }

    /**
     * 获取教练详细信息
     */
    @PreAuthorize("@ss.hasPermi('puyu:coach:query')")
    @GetMapping(value = "/user/{userId}")
    public AjaxResult getInfoByUserId(@PathVariable("userId") Long userId)
    {
        return success(puyuCoachService.selectPuyuCoachByCoachId(userId));
    }

    /**
     * 新增教练
     */
    @PreAuthorize("@ss.hasPermi('puyu:coach:add')")
    @Log(title = "教练", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PuyuCoach puyuCoach)
    {
        return toAjax(puyuCoachService.insertPuyuCoach(puyuCoach));
    }

    /**
     * 修改教练
     */
    @PreAuthorize("@ss.hasPermi('puyu:coach:edit')")
    @Log(title = "教练", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PuyuCoach puyuCoach)
    {
        return toAjax(puyuCoachService.updatePuyuCoach(puyuCoach));
    }

    /**
     * 删除教练
     */
    @PreAuthorize("@ss.hasPermi('puyu:coach:remove')")
    @Log(title = "教练", businessType = BusinessType.DELETE)
	@DeleteMapping("/{coachIds}")
    public AjaxResult remove(@PathVariable Long[] coachIds)
    {
        return toAjax(puyuCoachService.deletePuyuCoachByCoachIds(coachIds));
    }

    /**
     * 获取教练用户列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:coach:list')")
    @GetMapping("/coachUserList")
    public AjaxResult coachUserList()
    {
        return success(puyuCoachService.selectCoachUserList());
    }

    /**
     * 获取教练用户列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:coach:list')")
    @GetMapping("/coachList")
    public AjaxResult coachList()
    {
        PuyuCoach puyuCoach = new PuyuCoach();
        boolean bolRole = SecurityUtils.hasRole(UserConstants.ROLE_COACH)
                || SecurityUtils.hasRole(UserConstants.ROLE_STUDENT)
                || SecurityUtils.hasRole(UserConstants.ROLE_PARENT);
        if (bolRole) {
            puyuCoach.setUserId(SecurityUtils.getUserId());
        }
        bolRole = SecurityUtils.hasRole(UserConstants.ROLE_MANAGER)
                || SecurityUtils.hasRole(UserConstants.ROLE_ADMIN);
        if (bolRole) {
            puyuCoach.setUserId(null);
        }
        return success(puyuCoachService.selectCoachList(puyuCoach));
    }

    /**
     * 获取教练用户列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:coach:list')")
    @GetMapping("/coachAllList")
    public AjaxResult coachAllList()
    {
        PuyuCoach puyuCoach = new PuyuCoach();
        boolean bolRole = SecurityUtils.hasRole(UserConstants.ROLE_COACH)
                || SecurityUtils.hasRole(UserConstants.ROLE_STUDENT)
                || SecurityUtils.hasRole(UserConstants.ROLE_PARENT);
        if (bolRole) {
            puyuCoach.setUserId(SecurityUtils.getUserId());
        }
        bolRole = SecurityUtils.hasRole(UserConstants.ROLE_MANAGER)
                || SecurityUtils.hasRole(UserConstants.ROLE_ADMIN);
        if (bolRole) {
            puyuCoach.setUserId(null);
        }
        return success(puyuCoachService.selectCoachAllList(puyuCoach));
    }
}
